<template>
  <div class="myorders-container">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <!-- 功能区域 -->
      <el-row :gutter="20">
        <!-- 根据id搜索 -->
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="initOrdersList">
            <el-button slot="append" icon="el-icon-search" @click="initOrdersList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 修改地址对话框 -->
      <el-dialog title="修改地址" :visible.sync="editDialogVisible" width="50%" :before-close="handleClose"
        @close="clearAddressForm">
        <el-form ref="addressFormRef" :rules="addressFormRules" :model="addressForm" label-width="100px">
          <el-form-item label="省市区/县" prop="address1">
            <el-cascader v-model="addressForm.address1" :options="cityData">
            </el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="address2">
            <el-input v-model="addressForm.address2"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="">确 定</el-button>
        </span>
      </el-dialog>


      <!-- 展示物流对话框 -->
      <el-dialog title="物流进度" :visible.sync="showDialogVisible" width="50%">
        <!-- 时间线 -->
        <el-timeline>
          <el-timeline-item v-for="(item, index) in progressInfo" :key="index" :timestamp="item.time">
            {{item.context}}
          </el-timeline-item>
        </el-timeline>
      </el-dialog>
      <!-- 订单列表区域 -->
      <el-table :data="ordersList" border style="width: 100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格（元）"></el-table-column>
        <el-table-column prop="pay_status" label="是否付款">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.pay_status==='1'">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">
            {{scope.row.create_time|dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip effect="dark" content="编辑" placement="top">
              <el-button type="primary" icon="el-icon-edit" @click="openEditDialog(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除" placement="top">
              <el-button type="success" icon="el-icon-location" @click="showProgress"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[1, 2, 5, 10]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total" background>
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
  import cityData from './citydata.js'
  export default {
    name: 'MyOrders',
    data() {
      return {
        queryInfo: {
          query: '',
          pagenum: 1,
          pagesize: 10
        },
        ordersList: [],
        total: 0,
        editDialogVisible: false,
        addressForm: {
          address1: [],
          address2: ''
        },
        addressFormRules: {
          address1: [{
            required: true,
            message: '请选择省市区/县',
            trigger: 'blur'
          }],
          address2: [{
            required: true,
            message: '请输入详细地址',
            trigger: 'blur'
          }]
        },
        cityData: cityData,
        showDialogVisible: false,
        progressInfo: []
      }
    },
    created() {
      this.initOrdersList()
    },
    methods: {
      async initOrdersList() {
        const {
          data: res
        } = await this.$http.get('orders', {
          params: this.queryInfo
        })
        if (res.meta.status !== 200) {
          this.$message({
            type: 'error',
            showClose: true,
            message: '获取数据失败',
          })
        } else {
          this.ordersList = res.data.goods
          this.total = res.data.total
        }
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.queryInfo.pagesize = val
        this.initOrdersList()

      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.queryInfo.pagenum = val
        this.initOrdersList()
      },
      openEditDialog(row) {
        this.editDialogVisible = true
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },
      clearAddressForm() {
        this.$refs.addressFormRef.resetFields()
      },
      async showProgress() {
        this.showDialogVisible = true
        const {
          data: res
        } = await this.$http.get('/kuaidi/1106975712663')
        if (res.meta.status !== 200) {
          this.$message({
            type: 'error',
            showClose: true,
            message: '获取数据失败',
          })
        } else {
          this.progressInfo = res.data
          console.log(this.progressInfo)
        }
      }
    },
  }

</script>
<style lang="less" scoped>
  .box-card {
    margin-top: 15px;
    padding: 0;
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.1) !important;
  }

  .el-pagination {
    margin-top: 15px
  }

  .el-cascader {
    width: 100%;
  }

</style>
